package com.suman.bbcticker.configure;

import java.util.Iterator;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;

import com.suman.bbcticker.ApplicationStateObjectsHolder;
import com.suman.bbcticker.Eula;
import com.suman.bbcticker.R;

public class FeedTickerPreferences extends PreferenceActivity {
	private static final String SELECTED_TEXT = "Value Selected is ";
	protected static final String TAG = "FeedTickerPreferences";
	private int mAppWidgetId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent launchIntent = getIntent();
		Bundle extras = launchIntent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

		}
		Eula.showEula(this);
		addPreferencesFromResource(R.xml.preferences);

		RssFeedPreference rssFeedPreference = ApplicationStateObjectsHolder.preferenceHashMap.get(mAppWidgetId);

		setupFeedRefreshTimePreference(rssFeedPreference);
		setupAlertTypeNotificationPreference(rssFeedPreference);
		setupSiteSelectionListPreference(rssFeedPreference);
		setupAlertTypeSoundPreference(rssFeedPreference);
		setupAlertTypeVibratePreference(rssFeedPreference);
		setupAlertTypeKeywordPreference(rssFeedPreference);
		setupWeatherSelectionGpsPreference(rssFeedPreference);
		setupWeatherSelectionCustomLocationPreference(rssFeedPreference);

	}
	
	private void setupWeatherSelectionGpsPreference(RssFeedPreference rssFeedPreference) {
		CheckBoxPreference weatherSelectionGpsPreference = (CheckBoxPreference) getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_GPS_CHECKBOXKEY);
		//weatherSelectionGpsPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		if(rssFeedPreference!=null){
			weatherSelectionGpsPreference.setChecked(rssFeedPreference.isGpsWeatherSelection());
			adjustPreferenceValuesForCheckBoxInWeatherSettings(rssFeedPreference.isGpsWeatherSelection(),weatherSelectionGpsPreference);
			//weatherSelectionGpsPreference.setSummary(SELECTED_TEXT + rssFeedPreference.isGpsWeatherSelection());
		}
	}
	
	
	
	private void setupWeatherSelectionCustomLocationPreference(RssFeedPreference rssFeedPreference) {
		EditTextPreference weatherSelectionCustomLocationEditTextPreference = (EditTextPreference) getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_CUSTOMLOCATION_EDITTEXTKEY);;
		weatherSelectionCustomLocationEditTextPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		
		CheckBoxPreference weatherSelectionCustomLocationCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_CUSTOMLOCATION_CHECKBOXKEY);		
		weatherSelectionCustomLocationCheckBoxPreference.setOnPreferenceChangeListener(getOnPreferenceChangedListenerForCheckBoxInWeatherSettings() );
		
		if(rssFeedPreference!=null){
			//adjustPreferenceValuesForGpsCheckBox(rssFeedPreference.getCustomLocationWeatherSelectionValue());
			weatherSelectionCustomLocationEditTextPreference.setText(rssFeedPreference.getCustomLocationWeatherSelectionValue());
			weatherSelectionCustomLocationEditTextPreference.setSummary(SELECTED_TEXT + rssFeedPreference.getCustomLocationWeatherSelectionValue());
			weatherSelectionCustomLocationCheckBoxPreference.setChecked(rssFeedPreference.isCustomLocationWeatherSelectionCheckBox());
			adjustPreferenceValuesForCheckBoxInWeatherSettings(rssFeedPreference.isCustomLocationWeatherSelectionCheckBox(),weatherSelectionCustomLocationCheckBoxPreference);
			
		}
		
	}

	private OnPreferenceChangeListener getOnPreferenceChangedListenerForCheckBoxInWeatherSettings() {
		OnPreferenceChangeListener onPreferenceChangeListener = new OnPreferenceChangeListener(){
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				adjustPreferenceValuesForCheckBoxInWeatherSettings(newValue,preference);
				//preference.setSummary(SELECTED_TEXT + newValue);
				return true;
			}

		};
		return onPreferenceChangeListener;
	}
	
	/**
	 * This method just enables/disables  checkboxes in whether settings preferences based on what users do
	 * in other checkbox and vice versa
	 * @param newValue
	 * @param preference 
	 */
	private void adjustPreferenceValuesForCheckBoxInWeatherSettings(Object newValue, Preference preference) {		
		CheckBoxPreference weatherSelectionCheckPreference = (CheckBoxPreference)  preference;
		if(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_GPS_CHECKBOXKEY.equals(weatherSelectionCheckPreference.getKey())){
			CheckBoxPreference weatherSelectionCustomLocationCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_CUSTOMLOCATION_CHECKBOXKEY);
			weatherSelectionCustomLocationCheckBoxPreference.setEnabled(!(Boolean)newValue);
			weatherSelectionCustomLocationCheckBoxPreference.setChecked(false);
		}else if(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_CUSTOMLOCATION_CHECKBOXKEY.equals(weatherSelectionCheckPreference.getKey())){
			CheckBoxPreference weatherSelectionGpsCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_WEATHERSELECTION_GPS_CHECKBOXKEY);
			weatherSelectionGpsCheckBoxPreference.setEnabled(!(Boolean)newValue);
			weatherSelectionGpsCheckBoxPreference.setChecked(false);
			
			
		}
		
	}
	
	private void setupAlertTypeKeywordPreference(RssFeedPreference rssFeedPreference) {
		Preference alertKeywordPreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_KEYWORD_KEY);
		alertKeywordPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			EditTextPreference editTextAlertKeywordPreference = (EditTextPreference) alertKeywordPreference;
			String alertKeyword = rssFeedPreference.getAlertKeyword();
			editTextAlertKeywordPreference.setText(alertKeyword);
			editTextAlertKeywordPreference.setSummary(SELECTED_TEXT + alertKeyword);

		}
		
	}

	private void setupSiteSelectionListPreference(RssFeedPreference rssFeedPreference) {
		Preference siteSelectionPreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_SITESELECTED_KEY);
		siteSelectionPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				AutoCompleteTextViewPreference siteSelectionPreference = (AutoCompleteTextViewPreference) preference;
				setSummaryForListPreference(siteSelectionPreference, (String) newValue);
				return true;
			}

		});
		
		Preference customUrlSiteSelectionPreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_SITESELECTED_CUSTOMURL_EDITTEXTKEY);
		customUrlSiteSelectionPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());

		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			AutoCompleteTextViewPreference siteSelectionListPreference = (AutoCompleteTextViewPreference) siteSelectionPreference;
			String selectedSite = rssFeedPreference.getSelectedSite();
			
			Iterator<String> iterator = ApplicationStateObjectsHolder.displayStringToSiteUrlMap.keySet().iterator();
			while(iterator.hasNext()){
				String key = iterator.next();
				String value = ApplicationStateObjectsHolder.displayStringToSiteUrlMap.get(key);
				if(value.equalsIgnoreCase(selectedSite)){
					siteSelectionListPreference.setText(key);
					
				}
			}
			setSummaryForListPreference(siteSelectionListPreference, siteSelectionListPreference.getText());
			
			EditTextPreference customUrlEditTextSiteSelectionPreference = (EditTextPreference) customUrlSiteSelectionPreference;
			String customUrlForSite = rssFeedPreference.getSelectedSite();
			customUrlEditTextSiteSelectionPreference.setText(customUrlForSite);
			customUrlEditTextSiteSelectionPreference.setSummary(SELECTED_TEXT + customUrlForSite);
		}
	}

	private void setSummaryForListPreference(AutoCompleteTextViewPreference siteSelectionListPreference, String selectedSite) {
		siteSelectionListPreference.setSummary(SELECTED_TEXT + selectedSite);
	}

	private void setupAlertTypeVibratePreference(RssFeedPreference rssFeedPreference) {
		Preference alertTypeVibratePreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_VIBRATE_KEY);
		alertTypeVibratePreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		CheckBoxPreference alertTypeVibrateNotificationPreference = (CheckBoxPreference) alertTypeVibratePreference;
		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			boolean vibrateNotification = rssFeedPreference.getVibrateNotification();
			alertTypeVibrateNotificationPreference.setChecked(vibrateNotification);
			alertTypeVibrateNotificationPreference.setSummary(SELECTED_TEXT + vibrateNotification);
		}
	}

	private void setupAlertTypeSoundPreference(RssFeedPreference rssFeedPreference) {
		Preference alertTypeSoundPreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_SOUND_KEY);
		alertTypeSoundPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		CheckBoxPreference alertTypeSoundNotificationPreference = (CheckBoxPreference) alertTypeSoundPreference;
		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			boolean soundNotification = rssFeedPreference.getSoundNotification();
			alertTypeSoundNotificationPreference.setChecked(soundNotification);
			alertTypeSoundNotificationPreference.setSummary(SELECTED_TEXT + soundNotification);

		}
	}

	private void setupAlertTypeNotificationPreference(RssFeedPreference rssFeedPreference) {
		Preference alertTypeNotificationPreference = getPreferenceScreen().findPreference(
				PreferenceLauncherActivity.TICKER_ALERTTYPE_NOTIFICATION_KEY);
		alertTypeNotificationPreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());
		CheckBoxPreference checkBoxAlertTypeNotificationPreference = (CheckBoxPreference) alertTypeNotificationPreference;
		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			boolean notificationEnabled = rssFeedPreference.getNotificationEnabled();
			checkBoxAlertTypeNotificationPreference.setChecked(notificationEnabled);
			checkBoxAlertTypeNotificationPreference.setSummary(SELECTED_TEXT + notificationEnabled);
		}
	}

	private void setupFeedRefreshTimePreference(RssFeedPreference rssFeedPreference) {
		Preference feedRefreshTimePreference = getPreferenceScreen().findPreference(PreferenceLauncherActivity.TICKER_ALERTTYPE_FEEDREFRESHTIME_KEY);
		feedRefreshTimePreference.setOnPreferenceChangeListener(onPreferenceChangeListenerImpl());

		/** This is for when user selects Edit Config functionality,the initial layout should get rid of the values
		 * which are set by default in Preferences.xml file but display the latest values which are being used in the app*/
		if (rssFeedPreference != null) {
			EditTextPreference editTextFeedRefreshTimePreference = (EditTextPreference) feedRefreshTimePreference;
			String feedRefreshTime = rssFeedPreference.getFeedRefreshTime();
			editTextFeedRefreshTimePreference.setText(feedRefreshTime);
			editTextFeedRefreshTimePreference.setSummary(SELECTED_TEXT + feedRefreshTime);

		}
	}

	private OnPreferenceChangeListener onPreferenceChangeListenerImpl() {
		return new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				preference.setSummary(SELECTED_TEXT + newValue);
				return true;
			}

		};
	}

}
